package com.tuo51.childcare.controller;

import com.tuo51.childcare.domain.carestore.StoreItem;
import com.tuo51.childcare.service.BizStoreItemService;
import com.tuo51.evaluate.domain.evaluatingV2.EvaluateV2Record;
import com.tuo51.evaluate.domain.news.ArticleListItem;
import com.tuo51.evaluate.repositories.AnswerNativeRepository;
import com.tuo51.evaluate.service.EvaluateV2ConclusionService;
import com.tuo51.evaluate.service.EvaluateV2Service;
import com.tuo51.evaluate.service.article.ArticleService;
import com.tuo51.evaluate.vo.evaluate.EvaluateV2AnswerVO;
import com.tuo51.foudation.model.StatusResponse;
import com.tuo51.foudation.utils.CopyUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.tuo51.foudation.constant.APPConstants.TagRelObjectType.QUESTION;
import static com.tuo51.foudation.constant.RequestConstant.APP_CHANNEL;

/**
 * 公众号查看儿心量表报告
 */

@RestController
@RequestMapping("/papi/pb/")
@Api(value = "/papi/pb/", tags = "家长服务号 - 儿心量表测评")
public class PAPIEvaluateController {


    @Autowired
    private EvaluateV2Service evaluateService;

    @Autowired
    private EvaluateV2ConclusionService conclusionService;

    @Autowired
    private AnswerNativeRepository answerRepository;

    @Autowired
    private ArticleService articleService;

    @Autowired
    private BizStoreItemService storeItemService;

    @ApiOperation(value = "儿心量表测评报告详情",httpMethod = "POST")
    @RequestMapping("/detail")
    public StatusResponse evaluateDetail(String evaluateRecordId,
                                         @RequestHeader(value = APP_CHANNEL, required = false) String channel) {

        EvaluateV2Record object = evaluateService.getEvaluateDetail(evaluateRecordId, channel);

        Map result = conclusionService.generateReportExtInfo(object);

        List<EvaluateV2AnswerVO> answerSheet = answerRepository.findAnswerSheet(evaluateRecordId);
        result.put("answerSheet", answerSheet);

        List<String> qids = answerSheet.stream().map( k -> k.getQid() ).collect(Collectors.toList());

//        Map<String, EvaluateV2AnswerVO> cache = new HashMap<>();
//        answerSheet.forEach(k -> {
//            EvaluateV2AnswerVO o = cache.get(k.getDimensionName());
//            if (o == null || o.getIndexOnPaper() < k.getIndexOnPaper()) {
//                cache.put(k.getDimensionName(), k);
//            }
//        });
//
//        List<String> qids = cache.values().stream().filter(k -> StringUtils.isNotEmpty(k.getQid())).map(k -> k.getQid()).collect(Collectors.toList());

        {
            //查询推荐文章
//        private List<ArticleListItem> recommendArticles;
            List<ArticleListItem> recommendArticles = articleService.getArticleItemsByObject(qids, QUESTION.getValue(), null);
            result.put("recommendArticles", recommendArticles);
        }

        {
            //查询推荐视频
//        private List<StoreItem> carePoolItems;
            List<StoreItem> recommendArticles = storeItemService.getStoreItemByObjects(qids, QUESTION.getValue(), null);
            result.put("carePoolItems", recommendArticles);
        }
        result.putAll(CopyUtils.transBean2Map(object));

        return StatusResponse.success(result);
    }

}
